package com.share.device.controller;

import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.device.domain.PowerBank;
import com.share.device.service.IPowerBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 充电宝相关业务 controller
 */
@Tag(name = "充电宝接口管理")
@RequestMapping("/powerBank")
@RestController
public class PowerBankController extends BaseController {

    @Autowired
    private IPowerBankService powerBankService;

    @Operation(summary = "查询充电宝列表")
    @GetMapping("/list")
    public TableDataInfo list(PowerBank powerBank){
        log.info("查询充电宝列表");
        startPage();
        List<PowerBank> powerBankList = powerBankService.selectPowerBankList(powerBank);
        return getDataTable(powerBankList);
    }

    @Operation(summary = "获取充电宝详细信息")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable Long id){
        log.info("获取充电宝详细信息:{}",id);

        //mp
        PowerBank powerBank = powerBankService.getById(id);

        return AjaxResult.success(powerBank);
    }

    @Operation(summary = "新增充电宝")
    @PostMapping
    public AjaxResult save(@RequestBody PowerBank powerBank){
        log.info("新增充电宝:{}",powerBank);

        int count = powerBankService.savePowerBank(powerBank);

        return toAjax(count);
    }

    @Operation(summary = "修改充电宝")
    @PutMapping
    public AjaxResult update(@RequestBody PowerBank powerBank){
        log.info("修改充电宝:{}",powerBank);

        int rows = powerBankService.updatePowerBank(powerBank);

        return toAjax(rows);
    }

    //可批量删除 逻辑删除
    @Operation(summary = "删除充电宝")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteBatch(@PathVariable List<Long> ids){
        log.info("批量删除充电宝:{}",ids);
        boolean isSuccess = powerBankService.removeBatchByIds(ids);
        return toAjax(isSuccess);
    }
}
